<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备编号" prop="innerCode">
        <el-input
          v-model="queryParams.innerCode"
          placeholder="请输入设备编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="virtualMachineList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" type="index" align="center" width="80px"/>
      <el-table-column label="设备编号" align="center" prop="innerCode" />
      <el-table-column label="设备容量" align="center" prop="channelMaxCapacity" />
      <el-table-column label="详细地址" align="center" prop="addr" show-overflow-tooltip="true"/>
      <el-table-column label="设备状态-(运营)" align="center" prop="vmStatus">
        <template #default="scope">
          <dict-tag :options="vm_status" :value="scope.row.vmStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="运行状态-(设备)" align="center" prop="runningStatus" >
        <template #default="scope">
          <span v-if="scope.row.runningStatus != null">
            {{ JSON.parse(scope.row.runningStatus).status? "运行中" : "停止" }}
          </span>
          <span v-else>异常...</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="showVMInfo(scope.row)" v-hasPermi="['manage:virtualMachine:query']">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
    </el-dialog>
  </div>
</template>

<script setup name="VirtualMachine">
import { listVirtualMachine, getVirtualMachine, delVirtualMachine, addVirtualMachine, updateVirtualMachine } from "@/api/manage/virtualMachine";
import { onMounted } from "vue";
import {listPartner} from "@/api/manage/partner";
import {listNode} from "@/api/manage/node";
import {listVmType} from "@/api/manage/vmType";

const { proxy } = getCurrentInstance();
const { vm_status } = proxy.useDict('vm_status');

const virtualMachineList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    innerCode: null,
    nodeId: null,
    addr: null,
    businessType: null,
    regionId: null,
    partnerId: null,
    vmTypeId: null,
    vmStatus: null,
    runningStatus: null,
  },
  rules: {
    channelMaxCapacity: [
      { required: true, message: "设备容量不能为空", trigger: "blur" }
    ],
    nodeId: [
      { required: true, message: "点位Id不能为空", trigger: "blur" }
    ],
    lastSupplyTime: [
      { required: true, message: "上次补货时间不能为空", trigger: "blur" }
    ],
    businessType: [
      { required: true, message: "商圈类型不能为空", trigger: "change" }
    ],
    regionId: [
      { required: true, message: "区域Id不能为空", trigger: "blur" }
    ],
    partnerId: [
      { required: true, message: "合作商Id不能为空", trigger: "blur" }
    ],
    vmTypeId: [
      { required: true, message: "设备型号不能为空", trigger: "blur" }
    ],
    vmStatus: [
      { required: true, message: "设备状态，0:未投放;1-运营;3-撤机不能为空", trigger: "change" }
    ],
    createTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询设备管理列表 */
function getList() {
  loading.value = true;
  listVirtualMachine(queryParams.value).then(response => {
    virtualMachineList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    innerCode: null,
    channelMaxCapacity: null,
    nodeId: null,
    addr: null,
    lastSupplyTime: null,
    businessType: null,
    regionId: null,
    partnerId: null,
    vmTypeId: null,
    vmStatus: null,
    runningStatus: null,
    longitudes: null,
    latitude: null,
    clientId: null,
    policyId: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("virtualMachineRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}


/** 提交按钮 */
function submitForm() {
  proxy.$refs["virtualMachineRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateVirtualMachine(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addVirtualMachine(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 查看详情按钮操作 */
function showVMInfo(row) {
  reset();
  const _id = row.id || ids.value
  getVirtualMachine(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "设备运营详情";
  });
}

// 获取合作商列表
const partnerList = ref([]);
const paramsAll = reactive({
  pageNum: 1,
  pageSize: 10000,
});


function getPartnerList() {
  listPartner(paramsAll).then(response => {
    partnerList.value = response.rows;
  });
}

//获取点位列表
const nodeList = ref([]);

function getNodeList() {
  listNode(paramsAll).then(response => {
    nodeList.value = response.rows;
  });
}
// 获取设备型号列表
const vmTypeList = ref([]);

function getVmTypeList() {
  listVmType(paramsAll).then(response => {
    vmTypeList.value = response.rows;
  });
}
onMounted(() => {
  getPartnerList();
  getNodeList();
  getVmTypeList();
});

getList();
</script>
